package com.auto.learning.sharesdk.tools;

import java.util.HashMap;

import android.content.Context;
import android.os.Handler.Callback;
import android.os.Message;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.utils.UIHandler;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

import com.auto.learning.bean.WeChatData;

/**
 * 第三方登录
 * @author wangjianjun
 */
public class SnsLogin implements PlatformActionListener {
	
	public static final int MSG_AUTH_COMPLETE = 5;

	private Callback callback;
	private Context context;
	
	public SnsLogin(Callback callback,Context context){
		this.callback = callback;
		this.context = context;
	}

	@Override
	public void onCancel(Platform arg0, int arg1) {
		
	}

	@Override
	public void onComplete(Platform platform, int action, HashMap<String, Object> res) {
		try {
			 if (action == Platform.ACTION_USER_INFOR) {
					Message message = new Message();
					if (platform instanceof Wechat) {
						WeChatData weChatData = new WeChatData(platform.getDb().getUserId(), res.get("nickname") + "", res.get("headimgurl").toString()+ "", 0);
						message.what = MSG_AUTH_COMPLETE;
						message.obj = weChatData;
						UIHandler.sendMessage(message, callback);
					} else if (platform instanceof QQ) {
						WeChatData weChatData = new WeChatData(platform.getDb().getUserId(), res.get("nickname") + "", res.get("figureurl_qq_1").toString()+ "", 1);
						message.what = MSG_AUTH_COMPLETE;
						message.obj = weChatData;
						UIHandler.sendMessage(message, callback);
					}
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onError(Platform arg0, int arg1, Throwable arg2) {
		
	}
	
	public void authorize(Platform plat) {
		if (plat.isValid()) {
			plat.removeAccount();
		}
		plat.setPlatformActionListener(this);
		plat.SSOSetting(false);
		plat.showUser(null);
	}
	
}
